package controller.auth;



import java.nio.charset.Charset;

import model.MGroup;
import model.MUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import common.interf.IErrorHandle;


import service.interf.IAuthority;

@Controller
@RequestMapping("/auth")
public class GroupController {
	@Autowired
	private IAuthority auth;
	@Autowired
	private IErrorHandle errorHandle;
	
	/**
	 * 查找所有组的所有权限
	 *
	 * @return JSONArray
	 * 2012-12-15下午07:49:28
	 * String
	 */
	@RequestMapping("/groups_authority.htm")
	@ResponseBody
	public String findAllGroupAuths() {
		String s = "";
		try {
			s = auth.getAllGroupsWithAuthority();
			//System.out.println(s);
		} catch (Exception e) {
			System.out.println("method : findAllGroupAuths exception");
			errorHandle.handleErr(e);
		}
		return s;
	}
	

	/**
	 * 查找用户组
	 * @param model
	 * @return
	 * 2012-12-15下午08:53:18
	 * String
	 */
	@RequestMapping("/find_user_groups.htm")
	@ResponseBody
	public String findUserGroups(MUser model) {
		String s = "";
		try {
			if(null != model.getUserName()) {
				s = auth.getUserGroups(model.getUserName());
			}
			System.out.println(s);
			
		} catch (Exception e) {
			System.out.println("method : findGroup exception");
			errorHandle.handleErr(e);
		}
		return s;
	}
	//=======================纯组操作======================
	/**
	 * 删除指定组	
	 *
	 * @param model
	 *        组
	 * @return
	 * 2012-12-15下午08:02:01
	 * String
	 */
	@RequestMapping("/del_group.htm")
	@ResponseBody
	public String delGroup(MGroup model) {
		String s = "";
		try {
			if(null != model.getGroupId()) {
				s = auth.delGroup(model.getGroupId());
			}
			System.out.println(s);
		} catch(Exception e) {
			System.out.println("me");
			errorHandle.handleErr(e);
		}
		return s;
	}
	/**
	 * 添加组
	 * @param model
	 * @return
	 * 2012-12-15下午08:21:19
	 * String
	 */
	@RequestMapping("/add_group.htm")
	@ResponseBody
	public String addGroup(MGroup model) {
		String s = "";
		try {
			s = auth.addGroup(model);
		} catch(Exception e) {
			System.out.println("method : addGroup Exception");
			errorHandle.handleErr(e);
		}
		return s;
	}
	
	/**
	 * 查找所有组
	 *
	 * @return
	 * 2012-12-15下午07:48:44
	 * String
	 */
	@RequestMapping("/find_all_groups.htm")
	@ResponseBody
	public String findAllGroups(){
		String s = "";
		try {
			 s = auth.getAllGroups(0, 0);
			System.out.println(s);
			
		} catch (Exception e) {
			System.out.println("method : findAllGroup");
			errorHandle.handleErr(e);
		}
		return s;
	}
	
	//=======================纯组操作结束======================
	/**
	 * 查找指定组
	 */
	@RequestMapping("find_group.htm")
	@ResponseBody
	public String findGroup(MGroup model) {
		String s = "";
		try {
			 s = auth.getGroup(model.getGroupId());
		} catch (Exception e) {
			errorHandle.handleErr(e);
		}
		return s;
	}
	
	/**
	 * 批量更新组
	 */
	@RequestMapping("/batch_update_groups.htm")
	@ResponseBody
	public String batchUpdateGroups(MGroup model) {
		//此处验证model
		String s = "";
		try {
			 s = auth.batchUpdateGroup(model.getDatas());
		} catch(Exception e) {
			System.out.println("method : batchUpdateGroups exception ");
			errorHandle.handleErr(e);
		}
		return s;
	}
	
	/**
	 * 更新组权限
	 */
	@RequestMapping("/update_group_authorities.htm")
	@ResponseBody
	public String updateGroupAuths(MGroup model) {
		String s = "";
		try {
			s = auth.updateGroupAuthority(model.getGroupId(), 
					model.getGroupAuthorities());
		} catch(Exception e) {
			System.out.println("method : updataGroupAuths exception");
			errorHandle.handleErr(e);
		}
		return  s;
	}
	
	/**
	 * 获取指定组权限
	 */
	@RequestMapping("/find_group_authorities.htm")
	@ResponseBody
	public String findGroupAuths(MGroup model){
		String s = "";
		try {
			if(null != model.getGroupId()) {
				s = auth.getAuthsByGroup(model.getGroupId());
				System.out.println(s);
			}
		} catch (Exception e) {
			System.out.println("method : findGroupAuths");
			errorHandle.handleErr(e);
		}
		return s;
	}

    
	/**
	 * 添加多个用户到组
	 */
	@RequestMapping("/add_users2group.htm")
	@ResponseBody
	public String addUsersToGroup(MGroup model) {
		String s = "";
		try {
			s = auth.addUsersToGroup(model.getUsers(), model.getGroupId());
		} catch (Exception e) {
			System.out.println("method ：addUsersToGroup exception");
			errorHandle.handleErr(e);
		}
		return s;
	}
	
	/**
	 * 从组中删除多个用户
	 * debug pass
	 */
	@RequestMapping("/del_usersFgroup.htm")
	@ResponseBody
	public String delUsersFromGroup(MGroup model) {
		String s = "";
		try {
			s = auth.removeUsersFromGroup(model.getUsers(), model.getGroupId());
		} catch(Exception e) {
			System.out.println("method : delUsersFromGroup exception");
			errorHandle.handleErr(e);
		}
		return s;
	}
	/**
	 * 删除用户所有组
	 */
	@RequestMapping("del_user_groups.htm")
	@ResponseBody
	public String delUserGroups(MGroup model) {
		String s = "";
		try {
			s = auth.removeUserGroups(model.getUserId());
		} catch (Exception e) {
			System.out.println("method : delUserGroups exception ");
			errorHandle.handleErr(e);
		}
		return s;
	}
	
	/** 
	 * 更新用户组
	 */
	@RequestMapping("/update_user_groups.htm")
	@ResponseBody
	public String updateUserGroups(MGroup model){
		String s = "";
		try {
			s = auth.updateUserGroups(model.getUserName(), model.getGroups());
		} catch(Exception e) {
			System.out.println("method : updateUserGroups");
			errorHandle.handleErr(e);
		}
		return s;
	}
	
	/**
	 * 获取组用户
	 */
	@RequestMapping("/find_group_users.htm")
	@ResponseBody
	public String findGroupUsers(MGroup model) {
		String s = "";
		try {
			s = auth.getGroupUsers(model.getGroupId());
		} catch (Exception e) {
			System.out.println("method : findGroupUsers");
			errorHandle.handleErr(e);
		}
		return s;
	}
}
